package be.lioche.soupe;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

public class Commands implements Listener{

	public Main plugin;
	public Commands(Main main) {
		this.plugin = main;
	}

	@EventHandler
	public void commandes(PlayerCommandPreprocessEvent e){
		Player p = e.getPlayer();
		String msg = e.getMessage();
		String[] args = msg.split(" ");
		Player target = Bukkit.getServer().getPlayer(args[2]);


		if(args[0].equalsIgnoreCase("/coins")){
			if(p.isOp()){
				if(args[1].equalsIgnoreCase("set")){
					if(p != target){
						plugin.getConfig().set(target.getName()+".Coins", Integer.valueOf(args[3]));
						plugin.saveConfig();
						p.sendMessage(Main.name +"§c"+target.getName()+"§f possède maintenant: §c" + plugin.getConfig().getInt(target.getName()+".Coins") + "§f coins !");
						target.sendMessage(Main.name+"§c"+ p.getName()+"§f vous a défini vos coins à:§c "+plugin.getConfig().getInt(target.getName()+".Coins") + "§f!");
					}else{
						plugin.getConfig().set(target.getName()+".Coins", Integer.valueOf(args[3]));
						plugin.saveConfig();
						p.sendMessage(Main.name+"Vous avez défini vos coins à:§c "+plugin.getConfig().getInt(target.getName()+".Coins") + "§f!");
					}
				}
				
				if(args[1].equalsIgnoreCase("give")){
					if(p != target){
						plugin.getConfig().set(target.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(target.getName()+".Coins")+args[3]));
						plugin.saveConfig();
						p.sendMessage(Main.name +"§c"+target.getName()+"§f possède maintenant: §c" + plugin.getConfig().getInt(target.getName()+".Coins") + "§f coins !");
						target.sendMessage(Main.name+"§c"+ p.getName()+"§f vous givé §c"+ args[3] +"§f coins !");
					}else{
						plugin.getConfig().set(target.getName()+".Coins", Integer.valueOf(args[3]));
						plugin.saveConfig();
						target.sendMessage(Main.name+"Vous vous êtes givé §c"+ args[3] +"§f coins !");
					}
				}

				if(args[1].equalsIgnoreCase("check")){

					p.sendMessage(Main.name +"§c"+target.getName()+"§f possède : §c" + plugin.getConfig().getInt(target.getName()+".Coins") + "§f coins !");

				}

				if(args[1].equalsIgnoreCase("help")){

					p.sendMessage(Main.name + "Commandes disponibles:");
					p.sendMessage("");
					p.sendMessage("/§ccoins set§f <§cplayer§f> <§ccoins§f>");
					p.sendMessage("/§ccoins check§f <§cplayer§f>");
					p.sendMessage("/§ccoins help");
					p.sendMessage("");

				}

			}else{
				p.sendMessage(Main.name+"No permission !");
			}
		}else if(args[0].equalsIgnoreCase("/shop")){
			Shop.openshop(p);
		}
	}
}